const checkBracePair = str => {
  const sign = '()[]';
  const s = [];
  for (let i = 0; i < str.length; i++) {
    if (!sign.includes(str[i])) {
      continue;
    }
    const val = str[i];
    switch (val) {
      case '(': {
        s.push(val);
        break;
      }
      case '[': {
        s.push(val);
        break;
      }
      case ')': {
        const map1 = s.pop();
        if (map1 !== '(') {
          return false;
        }
        break;
      }
      case ']': {
        const map2 = s.pop();
        if (map2 !== '[') {
          return false;
        }
        break;
      }
      default: break;
    }
  }
  if (s.length) {
    return false; //
  } else {
    return true; // '符号正确'
  }
};

const checkLeftBrace = lineContent => {
  // 左括号数组
  const leftBraceArr = lineContent.match(/\(/g);
  if (leftBraceArr && leftBraceArr.length === 1) {
    return true;
  } else {
    return false;
  }
};

const chackLeftParenthesis = (lineContent, fnName, fnIndex) => {
  // 函数左边是否是左括号
  const isLeftParenthesis = lineContent.charAt(fnIndex + fnName.length) === '(';
  if (isLeftParenthesis) {
    return true;
  } else {
    return false;
  }
};

const checkBrace = (lineContent, fnName, fnIndex) => {
  const stateCheckBracePair = checkBracePair(lineContent);
  const stateCheckLeftBrace = checkLeftBrace(lineContent);
  const stateChackLeftParenthesis = chackLeftParenthesis(lineContent, fnName, fnIndex);
  return stateCheckBracePair && stateCheckLeftBrace && stateChackLeftParenthesis;
};
export { checkBrace };
